package com.wkq.jdbctemplate.commons;

/**
 * Created by yuj-b on 2016/5/27.
 */
abstract class ValueMapping {

    private static final String WILDCARD = "*" ;

/*    public static void main(String args) {
        ValueMapping mapping = ValueMapping.OneToOne("xiaomin", "小明", "lisi", "李四", "*", "UNKNOWN");
        System.out.println(mapping.get("xiaomin"));
        System.out.println(mapping.get("wangwu"));
    }*/

    public static ValueMapping OneToOne(Object ...values) {
        return new OneToOneMapping(values);
    }

    abstract Object get(Object key);

    public static class OneToOneMapping extends ValueMapping {

        private Object[] values = null ;

        private OneToOneMapping(Object ...values) {
            assert values!=null ;
            assert values.length % 2 == 0 ;

            this.values = values ;
        }

        @Override
        public Object get(Object key) {

            Object wildcardValue = null ;

            for (int i = 0; i < values.length ; i+=2) {
                if (values[i].equals(key)) {
                    return values[i+1];
                } else if (values[i].equals(WILDCARD)) {
                    wildcardValue = values[i+1];
                }
            }

            return wildcardValue ;
        }

    }

}